<?php

namespace app\admin\controller\project;

use app\common\controller\Backend;

class Order extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */
    protected $model = null;
    protected $relationSearch = true;
    // protected $searchFields = ['order.status', 'title', 'cate.name', 'price'];

    // 初始化
    public function __construct()
    {
        parent::__construct();
        // 引入的模型
        $this->ProjectModel = model('Project.Project');
        $this->OrderModel = model('Project.Order');
        $statusList = [
            '0' => '未开始',
            '1' => '开始报名',
            '2' => '截至报名',
            '3' => '开始活动',
            '4' => '结束活动',
        ];
        $OrderStatus = [
            '0' => '未开始',
            '1' => '已开始',
            '2' => '已结束',
        ];
        $this->assign([
            'statusList' => $statusList,
            'OrderStatus' => $OrderStatus,
        ]);

    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->OrderModel
                // ->alias('order')
                ->with(['business'])
                ->where($where)
                ->order($sort, 'asc')
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->OrderModel
                ->with(['business'])
                ->where($where)
                ->order($sort, 'asc')
                ->limit($offset, $limit)
                ->select(); //查询数据
            // halt(collection($list)->toArray());
        
            foreach($list as $item)
            {
                $project=$this->ProjectModel->find($item['pid']);
                $item['project']=$project;
            }
            // echo   $this->OrderModel->getlastSql();exit;
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $ids = !empty($ids) ? explode(',', $ids) : [];
        $row = $this->OrderModel->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__("没有找到该活动"));
                exit;
            }
        }
        
        $result = $this->OrderModel->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->OrderModel->getError());
            exit;
        }
        $this->success();
        exit;
    }
}